<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link type="text/css" rel="stylesheet" href="/webjars/bootstrap/3.3.7/css/bootstrap.css">
    <script type="text/javascript" src="/webjars/jquery/2.2.4/jquery.js"></script>
    <script type="text/javascript" src="/webjars/vue/2.6.10/dist/vue.js"></script>
    <script type="text/javascript" src="/webjars/axios/0.19.0/dist/axios.js"></script>
    <script type="text/javascript" src="/webjars/bootstrap/3.3.7/js/bootstrap.js"></script>
</head>
<body>
<div id="vuebox">
    <table class="table table-bordered container">
        <thead>
        <tr>
            <th>编号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>年龄</th>
            <th>等级</th>
            <th>所在部门</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="item in personList">
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>
                <p v-if="item.gender==1">男</p>
                <p v-if="item.gender==0">女</p>
            </td>
            <td>{{item.age}}</td>
            <td>
                <p v-if="item.rank==1">一级</p>
                <p v-if="item.rank==2">二级</p>
                <p v-if="item.rank==3">三级</p>
            </td>
            <td>{{item.department}}</td>
            <td>
                <button class="btn btn-danger" @click="delPerson(item.id)">删除</button>
            </td>
        </tr>
        </tbody>
    </table>
    <div class="col-md-6 text-right">
        <a class="btn btn-primary" @click="showModal()">新增信息</a>
    </div>
    <br>

    <!--模态框-->
    <div class="modal" id="mymodal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" type="button" >&times;</button>
                    <div class="modal-title">
                        添加人员
                    </div>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">
                        <div class="form-group">
                            <label for="name" class="col-sm-2 control-label">姓名</label>
                            <div class="col-sm-10">
                                <input v-model="new_person.name" id="name" type="text" class="form-control" placeholder="请输入姓名">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="gender" class="col-sm-2 control-label">性别:</label>
                            <div class="col-sm-10">
                                <select id="gender" class="form-control" v-model="new_person.gender">
                                    <option value="-1">请选择</option>
                                    <option value="0">女</option>
                                    <option value="1">男</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="age" class="col-sm-2 control-label">年龄:</label>
                            <div class="col-sm-10">
                                <input v-model="new_person.age" id="age" type="text" class="form-control" placeholder="请输入年龄">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="rank" class="col-sm-2 control-label">等级:</label>
                            <div class="col-sm-10">
                                <select id="rank" class="form-control" v-model="new_person.rank">
                                    <option value="0">请选择</option>
                                    <option value="1">一级</option>
                                    <option value="2">二级</option>
                                    <option value="3">三级</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="department" class="col-sm-2 control-label">所在部门:</label>
                            <div class="col-sm-10">
                                <input v-model="new_person.department" id="department" type="text" class="form-control" placeholder="请输入所在部门">
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-default" @click="hideModal(),clearForm()">关闭</button>
                    <button class="btn btn-primary" @click="saveModal()">保存</button>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
<script type="text/javascript">
    var vm=new Vue({
        el:"#vuebox",
        data:{
            personList:[],
            delResult:{},
            new_person:{name:'',gender:-1,age:null,rank:0,department:''}
        },
        methods:{
            select:function () {
                axios({url:"http://localhost:8080/selectPerson",method:"get"})
                    .then(res=>{this.personList=res.data.person})
            },
            delPerson:function (id) {
                        axios.get("http://localhost:8080/delPersonById",{params:{id:id}}).then(res=>{
                            this.delResult=res.data;
                            if (this.delResult.result=="SUCCESS"){
                                this.select(1);
                                alert("删除成功")
                            }else{
                                alert("删除失败")
                            }
                        });
            },
            showModal:function () {
                $('#mymodal').modal('show');
            },
            hideModal:function () {
                $('#mymodal').modal('hide');
            },
            clearForm:function () {
                $('#name').val('');
                $('#gender').val(-1);
                $('#age').val('');
                $('#rank').val(0);
                $('#department').val('');
            },
            saveModal:function () {
        axios.post("http://localhost:8080/save",this.new_person).then(res=>{
            if(res.data.result=="SUCCESS"){
                this.select(1);
                this.hideModal();
                this.clearForm();
                alert("添加成功")
            }else{
                alert("添加失败")
            }
        });
            }

        },
        mounted(){
            this.select();
        }
    })
</script>